package cc.rengu.igas.bsps.core.service.trans;


import cc.rengu.igas.bsps.common.constant.DubboMethodConstant;
import cc.rengu.igas.bsps.common.dao.AcctIssuerQueryMapper;
import cc.rengu.igas.bsps.common.dao.BspsManagerInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsUserInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.AcctIssuerQueryMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsManagerInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsUserInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.ManagerInfo;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.entity.UserInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.AcctIssuerQueryRequest;
import cc.rengu.igas.bsps.facade.response.AcctIssuerQueryResponse;
import cc.rengu.igas.share.common.util.GlobalEsbUtil;
import cc.rengu.igas.smps.facade.base.Header;
import cc.rengu.igas.smps.facade.request.BankAcctInfoQueryRequest;
import cc.rengu.igas.smps.facade.response.BankAcctInfoQueryResponse;
import cc.rengu.igas.smps.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.BinInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 账户发卡机构查询
 * Created by yuhongwen
 */
public class AcctIssuerQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        AcctIssuerQueryRequest acctIssuerQueryRequest = new AcctIssuerQueryRequest();
        ConvertUtil.convertOutput(acctIssuerQueryRequest);
        return acctIssuerQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        AcctIssuerQueryRequest acctIssuerQueryRequest = (AcctIssuerQueryRequest) request;
        AcctIssuerQueryResponse acctIssuerQueryResponse = new AcctIssuerQueryResponse();

        String instId = acctIssuerQueryRequest.getHeader().getInstId();
        String userId = acctIssuerQueryRequest.getUserId();
        String acctNo = acctIssuerQueryRequest.getAcctNo();
        rglog.info("必填性校验");
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(acctNo)) {
            rglog.error("必填性字段交易失败！userId:<{}>,acctNo:<{}>", userId, acctNo);
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        //查询用户信息
        rglog.info("查询用户信息");
        BspsUserInfoMapper bspsUserInfoMapper = new BspsUserInfoMapperImpl();
        UserInfo userInfo = bspsUserInfoMapper.selectBspsUserInfoByUserId(instId, userId);
        if (null == userInfo) {
            rglog.error("用户信息查询失败");
            throw new BizException(RespCodeEnum.GET_USER_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_INFO_ERROR.getRespDesc());
        }
        rglog.info("查询用户商户绑定信息");
        BspsMchntBindInfoMapper mchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
        List<MchntBindInfo> mchntBindList = mchntBindInfoMapper.selectBspsMchntBindInfoListByUserId(instId, userId);
        if (CollectionUtils.isEmpty(mchntBindList)) {
            rglog.info("查询用户拓展经理绑定信息");
            ManagerInfo managerInfo = null;
            BspsManagerInfoMapper managerInfoMapper = new BspsManagerInfoMapperImpl();
            if ("APP".equals(acctIssuerQueryRequest.getHeader().getChanlId())){
                managerInfo = managerInfoMapper.selectBspsManagerInfoById(instId,userId);
            }else {
                managerInfo = managerInfoMapper.selectBspsManagerInfoByOpenId(instId, userInfo.getOpenId());
            }
            if (null == managerInfo) {
                rglog.error("查询用户绑定拓展经理信息失败");
                throw new BizException(RespCodeEnum.ILLEGAL_OPERATION.getRespCode(), RespCodeEnum.ILLEGAL_OPERATION.getRespDesc());
            }
        }
        rglog.info("根据卡bin查询相关信息");
        AcctIssuerQueryMapper acctIssuerQueryMapper = new AcctIssuerQueryMapperImpl();
        BinInfo binInfo = acctIssuerQueryMapper.selectBinInfo(acctNo);
        if (null != binInfo) {
            acctIssuerQueryResponse.setCardType(binInfo.getCardType());
            acctIssuerQueryResponse.setIssrInstId(binInfo.getIssuerCode());
            acctIssuerQueryResponse.setIssrName(binInfo.getIssuerName());
            //查询该法人行的银联机构号
            SysParamService sysParamService = new SysParamServiceImpl();
            SysParam sysParam = sysParamService.getSysParamInfo(instId, "OPMS_SYS_PARAM", "CORPORATION_ORG_CODE");

            if (binInfo.getIssuerCode().substring(0, 4).equals(sysParam.getParamValue().substring(0, 4))) {
                /* 本行卡 */
                acctIssuerQueryResponse.setBankCardFlag("Y");

                BankAcctInfoQueryRequest coreAcctInfoQueryRequest = new BankAcctInfoQueryRequest();
                Header acctInfoHeader = new Header();
                acctInfoHeader.setInstId(instId);
                acctInfoHeader.setSrcSysId(AppParamConstant.SYS_ID);
                acctInfoHeader.setChanlId(ChannelEnum.BSPS.getChannelType());
                acctInfoHeader.setTransDate(DateUtil.getCurrentDate());
                acctInfoHeader.setTransTime(DateUtil.getCurrentTime());
                acctInfoHeader.setVersion("1.0.0");
                acctInfoHeader.setBizType("00");
                acctInfoHeader.setTxnNum("00010000200000");
                acctInfoHeader.setTraceNo(GlobalEsbUtil.generateEsbSeqeuenceNumber(instId));
                coreAcctInfoQueryRequest.setAcctNo(acctNo);
                coreAcctInfoQueryRequest.setHeader(acctInfoHeader);
                DubboService dubboService = new DubboServiceImpl();
                Result<BankAcctInfoQueryResponse> result = (Result<BankAcctInfoQueryResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_BANK_SUP_SERVICE, "bankAcctInfoQuery", coreAcctInfoQueryRequest);
                if (null == result) {
                    rglog.error("调用通道查询账户信息失败");
                } else if (!result.isSuccess() || null == result.getResult() || !OltpRpcdEnum.TRANS_SUCCESS.getRespCode().equals(result.getResult().getRespCode())) {
                    rglog.error("调用通道查询账户信息失败");
                } else {
                    //交易成功
                }
            } else {
                /* 他行卡 */
                acctIssuerQueryResponse.setBankCardFlag("N");
            }
        } else {
            rglog.error("未查询到账户发卡机构信息!");
        }
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(acctIssuerQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        AcctIssuerQueryResponse acctIssuerQueryResponse = (AcctIssuerQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(acctIssuerQueryResponse);
    }
}
